<?php
class Controller_Article extends Controller
{
	public $tbl_content = 'article_content';
	
	public function initialize() 
	{
		$this->mo = new Model_Article();
	}
	
	public function index() 
	{
	}
	
	/**
	 * 添加/修改页面
	 */
	public function detail() 
	{
		// 限制表单上传的最大值
		$this->MAX_FILE_SIZE = 1024000;
	}
	
	/**
	 * 添加/修改文章
	 */
	public function save() 
	{
		if (isset($_POST['id']) && !ctype_digit($_POST['id'])) {
			$this->failure(Core::getLang('invalid_request'));
		}
		// 添加文章时要自动设置创建时间
		if (empty($_POST['id'])) {
			$_POST['create_time'] = time();
		}
		// 如果有文件上传
		if ($_FILES['title_image']['error'] != UPLOAD_ERR_NO_FILE) {
			$options = array(
				'type'		=> 'image',
				'max_size'	=> 1024000,
				'save_path'	=> IMG_PATH.'article/',
				'save_rule'	=> '##Ymd/',
			);
	    	$upload = new UploadFile($options);
	    	// 上传错误提示错误信息
	    	if(!$upload->upload()) {
	    		$this->failure($upload->error());
	    	} else {// 上传成功 获取上传文件信息
	    		$_POST['title_image'] = $upload->fileList['title_image']['savename'];
	    	}
		}
		
		$_POST['editor_uid'] = $_SESSION['uid'];
		$_POST['editor'] = $_SESSION['username'];
		
// 		if (!isset($_POST['brief'])) {
// 			$_POST['brief'] = '';
// 		}
// 		$_POST['content'] = '';
		
		if ($this->mo->save()) {
			$model = new Model($this->tbl_content);
			if (empty($_POST['id'])) {
				$data = array(
					'id'		=> $this->mo->lastInsertId,
					'content'	=> $_POST['content']
				);
				$model->add($data);
			} else {
				$data = array(
					'content'	=> $_POST['content']
				);
				$model->update($data, 'id='.$_POST['id']);
			}
			exit;
// 			$this->success(Core::getLang('handle_success'));
		} else {
			exit;
// 			$this->failure(Core::getLang('save_article_data_fail'));
		}
	}
}